const express = require("express");
const app = express();
const mysql = require("mysql");
const bodyParser = require("body-parser");
const path = require("path");
const api = require("./api/index");


//=========mysql数据库================
var connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: "busdispatch",
  multipleStatements: true,
});
connection.connect();
//====================================


//============中间件===================
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.all("*", (req, res, next) => {
  res.header("Access-Control-Allow-Origin","*");
  res.header("Access-Control-Allow-Headers","content-type");
  res.header("Access-Control-Allow-Methods","GET,PUT,POST,DELETE,OPTIONS");
  if ( req.method.toLowerCase() == 'options' ) {
    res.sendStatus(200);
  } else {
    console.log(req.path + "\t\t\thost == " + req.ip);
    next();
  }
});
//====================================


//==============api===================
app.get("/test", (req, res) => {
  res.status(200).json({
    status: "success",
    data: {
      title: "hello",
      content: "test",
    },
  });
});

app.get("/api/getbusinfo", (req, res) => api.getbusinfo(connection, req, res));
app.post("/api/addbus", (req, res) => api.addbus(connection, req, res));
app.post("/api/deletebus", (req, res) => api.deletebus(connection, req, res));
app.post("/api/modifybusinfo", (req, res) => api.modifybusinfo(connection, req, res));
app.get("/api/getstationinfo", (req, res) => api.getstationinfo(connection, req, res));
app.post("/api/addstation", (req, res) => api.addstation(connection, req, res));
app.post("/api/deletestation", (req, res) => api.deletestation(connection, req, res));
app.post("/api/modifystationinfo", (req, res) => api.modifystationinfo(connection, req, res));
app.get("/api/getadmininfo", (req, res) => api.getadmininfo(connection, req, res));
app.post("/api/login", (req, res) => api.login(connection, req, res));
app.post("/api/register", (req, res) => api.register(connection, req, res));
app.post("/api/modifypassword", (req, res) => api.modifypassword(connection, req, res));
app.post("/api/deleteadmin", (req, res) => api.deleteadmin(connection, req, res));
app.get("/api/allline", (req, res) => api.allline(connection, req, res));
app.get("/api/getlineinfo", (req, res) => api.getlineinfo(connection, req, res));
app.post("/api/modifybusinline", (req, res) => api.modifybusinline(connection, req, res));
app.post("/api/deletebusinline", (req, res) => api.deletebusinline(connection, req, res));
app.post("/api/addline", (req, res) => api.addline(connection, req, res));
app.post("/api/addstationinline", (req, res) => api.addstationinline(connection, req, res));
app.post("/api/deleteline", (req, res) => api.deleteline(connection, req, res));
app.post("/api/deletestationinline", (req, res) => api.deletestationinline(connection, req, res));
app.post("/api/modifylineinfo", (req, res) => api.modifylineinfo(connection, req, res));
app.get("/api/getlinedata", (req, res) => api.getlinedata(connection, req, res));
app.get("/static/busicon", (req, res) => res.sendFile(path.join(path.join(__dirname, "static"), "bus.png")));
app.get("/static/stationicon", (req, res) => res.sendFile(path.join(path.join(__dirname, "static"), "station.png")));
//===================================


//===========error=============
app.use((err, req, res, next) => {
  console.error(err);
  res.status(500);
});

app.listen(3500, () => {
  console.log("Listen at " + 3500);
});